const fileService = require('../service/file.service')
const { FILE_PATH } = require('../constants/file-path')
const { APP_HOST, APP_PORT } = require('../app/config')
const fs = require('fs')
var iconv = require('iconv-lite')
const userService = require('../service/user.service')
class FileController {
    async saveAvatarInfo(ctx, next) {
        // console.log("保存头像信息")   
        const { filename, mimetype, size } = ctx.req.file
        const { id } = ctx.user
        const result = await fileService.createAvatar(filename, mimetype, size, id)
        //将头像id保存在user表中
        const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`
        await userService.updateAvatarUrlById(avatarUrl, id)
        ctx.body = result
    }
    async savePictureInfo(ctx, next) {
        const files = ctx.req.files

        const { id } = ctx.user
        const { momentId } = ctx.query
        //将多个文件信息存放到数据库中
        for (let file of files) {
            let { filename, mimetype, size } = file

            filename = `${APP_HOST}:${APP_PORT}/moment/image/${filename}`
            await fileService.createFile(filename, mimetype, size, id, momentId)
        }
        ctx.body = '动态图片上传成功'
    }
    async returnFileInfo(ctx, next) {
        const files = ctx.req.files



        //将多个文件信息存放到一个数组中
        let fileArray = []
        for (let file of files) {
            let { filename, mimetype, size } = file

            filename = `${APP_HOST}:${APP_PORT}/moment/image/${filename}`

            await fileService.createFile(filename, mimetype, size)

            fileArray.push(filename)

        }


        ctx.body = fileArray
    }
    async saveFileInfo(ctx, next) {
        const files = ctx.req.files
        const { originalname } = ctx.req.body

        //将多个文件信息存放到一个数组中
        let fileArray = []
        for (let file of files) {
            let { filename, mimetype, size } = file
            filename = `${APP_HOST}:${APP_PORT}/upload/file/${filename}`
            try {
                await fileService.createOtherFile(filename, mimetype, size, originalname)
            } catch (err) {
                console.log(err)
            }
            fileArray.push(filename)

        }


        ctx.body = fileArray
    }
    async otherFileInfo(ctx, next) {
        let { filename } = ctx.params
        const fullfilename = `${APP_HOST}:${APP_PORT}/upload/file/${filename}`
        const fileInfo = await fileService.getOtherFileByFilename(fullfilename)
        // console.log(fileInfo)
        // console.log(FILE_PATH)


        ctx.response.set('content-type', fileInfo.mimetype)
        ctx.body = fs.createReadStream(`${FILE_PATH}/${filename}`)

    }
    async otherFileInfoList(ctx, next) {
        const fileInfoList = await fileService.getOtherFileList()
        ctx.body = fileInfoList
    }
    async setCodingType(ctx, next) {
        try {
            // ctx.request.charset = 'utf-8'
            await next()
        } catch (err) {
            console.log(err)
        }
    }
}
module.exports = new FileController()